package com.haoyun.mirage.user.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haoyun.common.constant.CommonConstant;
import com.haoyun.common.controller.BaseController;
import com.haoyun.common.core.Result;
import com.haoyun.mirage.user.common.utils.TreeUtil;
import com.haoyun.mirage.user.model.entity.SysDict;
import com.haoyun.mirage.user.model.vo.DictTree;
import com.haoyun.mirage.user.model.vo.SysDictVO;
import com.haoyun.mirage.user.service.ISysDictService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author twg
 * @since 2018-05-30
 */
@RestController
@RequestMapping("/dict")
public class SysDictController extends BaseController {

    @Autowired
    ISysDictService sysDictService;


    /**
     * @author yanhao
     * @date 2018/7/2 13:32
     * 获取数据字典树
     * @return
     */
    @GetMapping(value = "/tree")
    public List<DictTree> getTree() {
        SysDict condition = new SysDict();
        //可以查询所有状态的
        //condition.setIsDeleted(CommonConstant.STATUS_NORMAL);
        return getDictTree(sysDictService.selectList(new EntityWrapper<>(condition)), 0L);
    }

    /**
     * @author yanhao
     * @date 2018/7/2 13:34
     * 删除数据字典
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> dict(@PathVariable Long id) {
        return Result.buildSuccessResult(sysDictService.deleteDict(id));
    }

   /* @GetMapping("/{id}")
    public Result<SysDict> getDictById(@PathVariable Long id) {
        return Result.buildSuccessResult(sysDictService.getDictById(id));
    }*/
   @GetMapping("/{id}")
   public SysDict getDictById(@PathVariable Long id) {
       return sysDictService.getDictById(id);
   }
    /**
     * @author yanhao
     * @date 2018/7/2 14:28
     * 添加数据字典
     * @return
     */
    @PostMapping("add")
    public Result<String> addDict(@RequestBody @Valid SysDictVO dict, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.buildErrorResult(fieldError.getDefaultMessage());
        }
        SysDict sysDict = new SysDict();
        BeanUtil.copyProperties(dict, sysDict);
        sysDictService.insertDict(sysDict);
        return Result.buildSuccessResult("添加成功");
    }

    /**
     * @author yanhao
     * @date 2018/7/2 14:30
     * 更新数据字典
     * @return
     */
    @PutMapping("update")
    public Result<String> updateDict(@RequestBody @Valid SysDictVO dict, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.buildErrorResult(fieldError.getDefaultMessage());
        }
        SysDict sysDict = new SysDict();
        BeanUtil.copyProperties(dict, sysDict);
        sysDictService.updateDictById(sysDict);
        return Result.buildSuccessResult("更新成功");
    }



    private List<DictTree> getDictTree(List<SysDict> sysDicts, long root) {
        List<DictTree> list = new ArrayList<>();
        DictTree tree = null;
        for (SysDict dict : sysDicts) {
            tree = new DictTree();
            tree.setId(dict.getId());
            tree.setKeyworld(dict.getKeyworld());
            tree.setParentId(dict.getParentId().longValue());
            tree.setValueDesc(dict.getValueDesc());
            tree.setClassLevel(dict.getClassLevel());
            tree.setClassOrder(dict.getClassOrder());
            tree.setClassType(dict.getClassType());
            list.add(tree);
        }
        return TreeUtil.build(list, root);
    }

}

